package com.upc.tp.timeplanning.controller;

import com.upc.tp.timeplanning.entity.*;
import com.upc.tp.timeplanning.mapper.ParentMapper;
import com.upc.tp.timeplanning.mapper.StudentMapper;
import com.upc.tp.timeplanning.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/parent", method = {RequestMethod.POST, RequestMethod.GET})
public class ParentController {

    @Autowired
    ParentMapper parentMapper;
    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/getAll")
    public List<Parent> getAll(){
        List<Parent> list = parentMapper.getAll();
        return list;
    }

    @RequestMapping("/showInformation") //通过用户名查询该家长用户信息
    public ResBody showInformation(@RequestBody Parent parent){
        List<Parent> list = parentMapper.showInformation(parent.getUsername());
        return new ResBody(100,"success",list);
    }

    @RequestMapping("/login")  //登录接口
    public ResBody login(@RequestBody Parent parent){

        ResBody resBody;
        try{
            Parent parent_temp = parentMapper.getByUsername(parent.getUsername());
            String pwd_temp = parent_temp.getPassword();
            if(pwd_temp.equals(parent.getPassword())){
//                Map<String,String> map=new HashMap<>();   //用于生成token，暂时用不着
//                map.put("username",parent_temp.getUsername());
//                String token= JwtUtil.getToken(map);
//                Map<String,String> resMap=new HashMap<>();
//                resMap.put("token",token);
                resBody = new ResBody(100,"success",null);
            }
            else{
                resBody = new ResBody(0,"fail",null);
            }
        }catch (Exception e){
            return new ResBody(1,"no_user",null);
        }
        return resBody;
    }

    @RequestMapping("/register")  //注册接口
    public ResBody insert(@RequestBody Parent parent) {

        ResBody res;
        try {
            parentMapper.insert(parent.getUsername(), parent.getPassword());
            res = new ResBody(100, "success", null);
        } catch (Exception e) {
            return new ResBody(0, "fail", null);
        }
        return res;
    }

    @RequestMapping("/update")  //更新用户名（昵称）和密码
    public ResBody update(@RequestBody Parent parent){

        ResBody res;
        try {
            parentMapper.update(parent.getParent_name(),parent.getPassword(),parent.getUsername());
            res = new ResBody(100, "success", null);
        } catch (Exception e) {
            return new ResBody(0, "fail", null);
        }
        return res;
    }

    @RequestMapping("/bind")  //绑定学生
    public ResBody bind(@RequestBody Parent parent){

        ResBody res;
        try {
            parentMapper.bind(parent.getStudent_username(),parent.getStudent_name(),parent.getUsername());
            res = new ResBody(100, "success", null);
        } catch (Exception e) {
            return new ResBody(0, "fail", null);
        }
        return res;
    }

}
